package com.bootdo.system.service.impl;

import com.bootdo.common.utils.R;
import com.bootdo.common.utils.StringUtils;
import com.bootdo.common.utils.UuidUtils;
import com.bootdo.system.dao.*;
import com.bootdo.system.domain.*;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bootdo.system.service.GuessService;



@Service
public class GuessServiceImpl implements GuessService {

	@Autowired
	private GuessDao guessDao;

	@Autowired
	private GuessItemDao guessItemDao;

	@Autowired
	private GuessLogDao guessLogDao;

	@Autowired
	private UserDao userDao;

	@Autowired
	private JournalDao journalDao;
	
	@Override
	public GuessDO get(Long id){
		GuessDO guessDO = guessDao.get(id);
		if(StringUtils.equals("1",guessDO.getGuessState())) {
			guessDO.setGuessStateName("未开始");
		}else if(StringUtils.equals("2",guessDO.getGuessState())) {
			guessDO.setGuessStateName("竞猜中");
		}else if(StringUtils.equals("3",guessDO.getGuessState())) {
			guessDO.setGuessStateName("已结束");
		}
		return guessDO;
	}
	
	@Override
	public List<GuessDO> list(Map<String, Object> map){
		return guessDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return guessDao.count(map);
	}
	
	@Override
	public R save(GuessDO guess){
		Date now = new Date();
		guess.setCreateDate(now);
		guess.setDelFlag("0");
		guess.setShowId(UuidUtils.getUUID());
		//1.未开始  2.竞猜中  3.已结束
		guess.setGuessState("1");
		guessDao.save(guess);
		if(!CollectionUtils.isEmpty(guess.getGuessItemDOList())) {
			GuessItemDO guessItemDO = new GuessItemDO();
			guessItemDO.setCreateDate(now);
			guessItemDO.setGuessId(guess.getId());
			guessItemDO.setDelFlag("0");
			guessItemDO.setGuessIntegral(0l);
			for(GuessItemDO data : guess.getGuessItemDOList()) {
				guessItemDO.setGuessName(data.getGuessName());
				guessItemDO.setGuessOdds(data.getGuessOdds());
				guessItemDao.save(guessItemDO);
			}
		}
		return R.ok();
	}
	
	@Override
	public R update(GuessDO guess){
		//1.未开始  2.竞猜中  3.已结束
		guess.setGuessState("1");
		guess.setShowId(UuidUtils.getUUID());
		guessDao.update(guess);
		if(!CollectionUtils.isEmpty(guess.getGuessItemDOList())) {
			GuessItemDO guessItemDO = new GuessItemDO();
			for(GuessItemDO data : guess.getGuessItemDOList()) {
				guessItemDO.setGuessName(data.getGuessName());
				guessItemDO.setGuessOdds(data.getGuessOdds());
				guessItemDO.setId(data.getId());
				guessItemDO.setGuessIntegral(0l);
				if(null != data.getId()) {
					guessItemDao.update(guessItemDO);
				}else {
					guessItemDO.setCreateDate(new Date());
					guessItemDO.setGuessId(guess.getId());
					guessItemDO.setDelFlag("0");
					guessItemDao.save(guessItemDO);
				}
			}
		}
		return R.ok();
	}
	
	@Override
	public int remove(Long id){
		return guessDao.remove(id);
	}
	
	@Override
	public int batchRemove(Long[] ids){
		return guessDao.batchRemove(ids);
	}

	/**
	 * 设置密码
	 * @param guess
	 */
	@Override
	public void setAnswer(GuessDO guess) {
		guessDao.update(guess);
	}

	@Override
	public R updateState(GuessDO guess) {
		guessDao.update(guess);
		if(StringUtils.equals("3",guess.getGuessState())) {
			//手动结束，产生流局，自动退还下注积分
			//获取竞猜记录
			Map<String,Object> map = new HashMap<String, Object>();
			map.put("showId",guess.getShowId());
			List<GuessLogDO> guessLogList = guessLogDao.list(map);
			if(!CollectionUtils.isEmpty(guessLogList)) {
				UserDO userDO = null;
				for(GuessLogDO data : guessLogList) {
					//获取账户积分余额
					userDO = new UserDO();
					userDO.setUserId(data.getCreateBy());
					UserDO userDO1 = userDao.get(userDO.getUserId());
					userDO.setIntegral(data.getIntegral());
					userDao.addIntegral(userDO);

					//更新竞猜信息
					//0.正确 1.错误 2.流局
					data.setGuessResult("2");
					data.setFinalIntegral(data.getIntegral());
					data.setAllIntegral(userDO1.getIntegral() + data.getIntegral());
					data.setGuessDescription(data.getGuessDescription() + ";产生流局退还下注的" + data.getIntegral() + "积分");
					guessLogDao.update(data);

					//保存积分流水
					GuessDO guessDO1 = guessDao.get(guess.getId());
					String strDateFormat = "yyyy-MM-dd HH:mm:ss";
					SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
					String startDate = sdf.format(guessDO1.getGuessStartDate());
					String endDate = sdf.format(guessDO1.getGuessEndDate());

					JournalDO journalDO2 = new JournalDO();
					journalDO2 = setData(userDO1);
					journalDO2.setJournalType("5");
					journalDO2.setJournalDescription("产生流局退还下注的" + data.getIntegral() + "积分");
					journalDO2.setIntegral(data.getIntegral());
					journalDO2.setServiceIntegral(0l);
					journalDO2.setAllIntegral(userDO1.getIntegral() + data.getIntegral());
					journalDO2.setShowId(guessDO1.getShowId());
					journalDO2.setGuessDetail("竞猜活动为开始时间为" + startDate + ",结束时间为" + endDate + "的" + guessDO1.getGuessTitle() + "活动");
					journalDao.save(journalDO2);
				}
			}
		}
		return R.ok();
	}

	private JournalDO setData(UserDO userDO) {
		JournalDO journalDO = new JournalDO();
		journalDO.setCreateBy(userDO.getUserId());
		journalDO.setUserId(userDO.getUserId());
		journalDO.setCreateDate(new Date());
		journalDO.setDelFlag("1");
		return journalDO;
	}

}
